package cn.yll.common.module;

import cn.yll.common.exception.BsErrEnum;
import com.alibaba.fastjson2.JSON;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * rest接口返回主数据结构
 * @author yangll
 * @param <T>
 */
@Getter
@Setter
@ApiModel("rest响应实体")
@Accessors(chain = true)
public class RestModel<T> implements Serializable {
    public static final Integer CODE_SUCCESS = 200;
    public static final Integer CODE_FAIL = 500;
    public static final String MESSAGE_SUCCESS = "请求成功";

    @ApiModelProperty("响应状态码 200成功 其它为异常")
    private Integer code;

    @ApiModelProperty("接口响应提示信息")
    private String message;

    @ApiModelProperty("请求响应数据")
    private T data;

    public RestModel() {
    }

    public RestModel(T data) {
        this.code = CODE_SUCCESS;
        this.message = MESSAGE_SUCCESS;
        this.data = data;
    }

    public RestModel(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public RestModel(Integer code, T data) {
        this.code = code;
        this.message = "";
        this.data = data;
    }

    public RestModel(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public RestModel(BsErrEnum errEnum) {
        this.code = errEnum.getCode();
        this.message = errEnum.getMessage();
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
